package Gui;

import java.awt.Color;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.Font;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;

import client.LibaryMain;

import Control.UserLibrarian;
import Logic.LoginInfo;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.ImageIcon;
/**
 * this class implements gui register new subscriber 
 * @author Ron Shenkerman
 * @author Gabi Revah
 * @author Aviad Almog
 * @author Ivan Onyshchenko
 */
public class Rigester extends JPanel {
	private JTextField UserNameTextField;
	private JTextField PasswordTextField;
	private String userRoll; 
	private JTextField textUserName;
	private JTextField textTelephone;
	private JTextField textEmail;
	/**
	 * Constructor
	 * @param userRoll
	 */
	public Rigester(String userRoll) {
		setBackground(new Color(153, 204, 255));
		this.userRoll= userRoll;
		
		initComponents();

	}
	 
    /**
	 * this function create instance of Registering new subscriber gui 
	 */
	public void  initComponents() {
		

		JLabel lblUserId = new JLabel("User ID");
		lblUserId.setBounds(286, 175, 57, 19);
		lblUserId.setFont(new Font("Tahoma", Font.BOLD, 15));
		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setBounds(286, 309, 73, 19);
		lblPassword.setFont(new Font("Tahoma", Font.BOLD, 15));
		
		UserNameTextField = new JTextField();
		UserNameTextField.setBounds(395, 174, 116, 22);
		UserNameTextField.setColumns(10);
		
		PasswordTextField = new JTextField();
		PasswordTextField.setBounds(395, 308, 116, 22);
		PasswordTextField.setColumns(10);
		
		JButton btnConfirm = new JButton("Confirm");
		btnConfirm.setIcon(new ImageIcon(Rigester.class.getResource("/Gui/images/finish.gif")));
		btnConfirm.setSelectedIcon(new ImageIcon(Rigester.class.getResource("/Gui/images/finish.gif")));
		btnConfirm.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 15));
		btnConfirm.setBounds(282, 341, 229, 38);
		
		btnConfirm.addActionListener(new java.awt.event.ActionListener() {     // Confirm Button in order to add new subscriber action listener 
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	btnConfirmActionPerformed(evt);
            
            }

					private void btnConfirmActionPerformed(ActionEvent evt) {
						// TODO Auto-generated method stub
						System.out.println(Login.un );
						Pattern p = Pattern.compile("@");
						Matcher m = p.matcher(textEmail.getText());
						Pattern p1 = Pattern.compile("[A-Z,a-z,&%$#@!()*^]");
						Matcher m1 = p1.matcher(textTelephone.getText());
						Pattern p2 = Pattern.compile("[A-Z,a-z,&%$#@!()*^]");
						Matcher m2 = p2.matcher(UserNameTextField.getText());
						if(UserNameTextField.getText().equals("")|textEmail.getText().equals("")|textUserName.getText().equals("")|textTelephone.getText().equals("")|PasswordTextField.getText().equals(""))
						{
							JOptionPane.showMessageDialog(null, "Registration wasn't entered correctly, you have empty fields please try again!");
						}
						
						else {
							if (!m.find()) {
											JOptionPane.showMessageDialog(null, "Please enter Email correctly");
											}
							else{
								
								if (m1.find()) {
												JOptionPane.showMessageDialog(null, "Please enter only numbers in Telefone field");
											   }
								
								else			{
												if (m2.find()) {
																JOptionPane.showMessageDialog(null, "Please enter only numbers in ID field");
																}
												else{
													UserLibrarian AddUser= new UserLibrarian(Login.un,Login.pas,"1",true);
													AddUser.AddNewSubscriber(UserNameTextField.getText(),textEmail.getText(),textUserName.getText(),textTelephone.getText() ,PasswordTextField.getText());
													}
												
												}
									
								}
							}
					}
				
        });
		
		JButton btnLogout = new JButton("Logout");
		btnLogout.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 13));
		btnLogout.setIcon(new ImageIcon(Rigester.class.getResource("/Gui/images/cancel.gif")));
		btnLogout.setBounds(720, 555, 111, 25);
		btnLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ArrayList<String> Arr=new ArrayList<String>();
				Arr.add("Logout");
				Arr.add("Worker");
				Arr.add(LoginInfo.UserName);
				LibaryMain.chat.handleMessageFromGui(Arr);
				MainScreen.cardLayot.show(MainScreen.card, "Login");
				
			}
		});
		
		JButton btnBack = new JButton("Back");
		btnBack.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 13));
		btnBack.setIcon(new ImageIcon(Rigester.class.getResource("/Gui/images/back.gif")));
		btnBack.setBounds(12, 555, 96, 25);
		
		
		btnBack.addActionListener(new java.awt.event.ActionListener() {     // Back button to Subscriber window 
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	btnConfirmActionPerformed(evt);
            
            }

					private void btnConfirmActionPerformed(ActionEvent evt) {
						// TODO Auto-generated method stub
						
			
							 MainScreen.cardLayot.show(MainScreen.card, userRoll);	
						
						
					}
				
        });
		setLayout(null);
		
		JLabel lblRigesterNewSubscriber = new JLabel("Rigester New Subscriber ");
		lblRigesterNewSubscriber.setBounds(28, 29, 257, 25);
		lblRigesterNewSubscriber.setFont(new Font("Tahoma", Font.BOLD, 20));
		add(lblRigesterNewSubscriber);
		add(btnConfirm);
		add(lblPassword);
		add(lblUserId);
		add(UserNameTextField);
		add(PasswordTextField);
		add(btnBack);
		add(btnLogout);
		
		JLabel lblUserName = new JLabel("User name");
		lblUserName.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblUserName.setBounds(287, 209, 89, 16);
		add(lblUserName);
		
		textUserName = new JTextField();
		textUserName.setBounds(395, 206, 116, 22);
		add(textUserName);
		textUserName.setColumns(10);
		
		JLabel lblTelephone = new JLabel("Telephone");
		lblTelephone.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblTelephone.setBounds(286, 245, 90, 16);
		add(lblTelephone);
		
		textTelephone = new JTextField();
		textTelephone.setBounds(395, 241, 116, 22);
		add(textTelephone);
		textTelephone.setColumns(10);
		
		JLabel lblEmail = new JLabel("Email");
		lblEmail.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblEmail.setBounds(287, 280, 56, 16);
		add(lblEmail);
		
		textEmail = new JTextField();
		textEmail.setBounds(395, 276, 116, 22);
		add(textEmail);
		textEmail.setColumns(10);
		
		JLabel label = new JLabel("");
		label.setIcon(new ImageIcon(Rigester.class.getResource("/Gui/images/contact-new.png")));
		label.setBounds(76, 176, 138, 152);
		add(label);
		
		
		
	}
}
